<?php
  include_once('./queries/utils.inc.php');

  function recencyQuery($connection, $index, $pn) {
    $sql = "SELECT c.datasource as source, c.lastupdate as recency " .
           "FROM currency c ";

    $rcount = get_recordcount($connection, $sql);
    
    $sql = $sql . "ORDER BY c.lastupdate " . get_limitclause($pn, $rcount);

    //echo $sql;
    unset($_SESSION["{$index}"]);
    $_SESSION["{$index}"] = array();
    $_SESSION["{$index}"]["title"]="Recency info for the job queue data source";        
    $_SESSION["{$index}"]["message"] = "Recency info for the job queue data source";
    
    $fields = array();
    $fields[0] = "source";
    $fields[1] = "recency";

    $rs = new recordset();
    $rs->__construct($rcount,
                   $fields,
                   get_recordset($connection, $sql));

    return $rs;
    
    //echo $sql;
  }

  function recencyExceptionQuery($connection, $index, $pn) {
    // we use 20 minutes as a cutoff for exceptional data sources for now
	  $sql = "SELECT * FROM currencies C " .
	          "WHERE C.lastupdate < (current_timestamp - " . 
                    get_interval(20, 'minute') . ")";

    $rcount = get_recordcount($connection, $sql);
    
    $sql = $sql . "ORDER BY C.lastupdate ";
    $sql = do_pagination_rewriting($pn, $rcount, $sql);

    unset($_SESSION["{$index}"]);
    $_SESSION["{$index}"] = array();
    $_SESSION["{$index}"]["title"]="Recency info for exceptional data sources";        
    $_SESSION["{$index}"]["message"] = "Recency info for exceptional data sources";
    
    $fields = array();
    $fields[0] = "source";
    $fields[1] = "recency";

    $rs = new recordset($rcount, $fields, get_recordset($connection, $sql));
    return $rs;
  }


  function leastRecentSource($connection) {
	  $sql = "SELECT min(datasource) FROM " . 
                  "(SELECT * FROM Currencies C " . 
                   "WHERE C.lastupdate >= (current_timestamp - " . 
                      get_interval(20, 'minute') . ")) C " . 
                 "WHERE lastupdate = " . 
                   "(SELECT min(lastupdate) FROM " . 
                     "(SELECT * from Currencies C " . 
                      "WHERE C.lastupdate >= (current_timestamp - " . 
                             get_interval(20, 'minute') . ")) C)";
	  return get_scalar(get_recordset($connection, $sql));
  }

  function leastRecency($connection) {
    $date = TS_FORMAT;
	  $sql = "SELECT to_char(min(lastupdate), '{$date}') FROM " . 
                  "(SELECT * FROM Currencies C " . 
                   "WHERE C.lastupdate >= (current_timestamp - " . 
                      get_interval(20, 'minute') . ")) C " . 
                 "WHERE lastupdate = " . 
                   "(SELECT min(lastupdate) FROM " . 
                     "(SELECT * FROM Currencies C " . 
                      "WHERE C.lastupdate >= (current_timestamp - " .
                             get_interval(20, 'minute') . ")) C)";
	  return get_scalar(get_recordset($connection, $sql));
  }

  function mostRecentSource($connection) {
	  $sql = "SELECT min(datasource) FROM ". 
                  "(SELECT * FROM Currencies C " . 
                   "WHERE C.lastupdate >= (current_timestamp - " .
                      get_interval(20, 'minute') . ")) C " . 
                 "WHERE lastupdate = " . 
                   "(SELECT max(lastupdate) FROM " . 
                     "(SELECT * FROM Currencies C " . 
                      "WHERE C.lastupdate >= (current_timestamp - " .
                             get_interval(20, 'minute') . ")) C)";
	  return get_scalar(get_recordset($connection, $sql));
  }

  function mostRecency($connection) {
    $date = TS_FORMAT;
	  $sql = "SELECT to_char(max(lastupdate), '{$date}') FROM " . 
                  "(SELECT * FROM Currencies C " . 
                   "WHERE C.lastupdate >= (current_timestamp - " . 
                      get_interval(20, 'minute') . ")) C " . 
                 "WHERE lastupdate = " . 
                   "(SELECT max(lastupdate) FROM " . 
                     "(SELECT * FROM Currencies C " . 
                      "WHERE C.lastupdate >= (current_timestamp - " . 
                             get_interval(20, 'minute') . ")) C)";
	  return get_scalar(get_recordset($connection, $sql));
  }
?>
